import { defineStore } from "pinia";
import {loginApi} from '@/apis/user.js'
import {ref} from 'vue'
import {useCartStore} from './cartStore.js'

export const useUserStore = defineStore('user',()=>{
    const cartStore = useCartStore();
    const userInfo = ref({});
    const getUserInfo = async({account,password})=>{
        const res  = await loginApi({account,password});
        userInfo.value = res.data.result
        //合并购物车
        cartStore.mergeCart();
        //获取最新购物车数据
        cartStore.updateNewsList();
    }
    const clearUserInfo = ()=>{
        userInfo.value={}
        cartStore.clearCart()
    }
    return {
        userInfo,
        getUserInfo,
        clearUserInfo
    }
},{
    persist:true
}
)